package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.SeckillGoodsMapper;
import com.qingcheng.pojo.seckill.SeckillGoods;
import com.qingcheng.pojo.seckill.SeckillOrder;
import com.qingcheng.service.seckill.SeckillOrderService;
import com.qingcheng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.Date;

/**
 * @author 岳剑飞
 * @Company http://www.ithiema.com
 */
@Service(interfaceClass = SeckillOrderService.class)
public class SeckillOrderServiceImpl implements SeckillOrderService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;
    @Autowired
    private IdWorker idWorker;

    /**
     * 添加订单
     *
     * @param id       商品id
     * @param time     商品秒杀开始时间
     * @param username 用户登录名
     * @return
     */
    @Override
    public Boolean add(Long id, String time, String username) {

        //获取商品数据
        SeckillGoods goods = (SeckillGoods) redisTemplate.boundHashOps("SeckillGoods_" + time).get(id);

        //如果没有库存,则直接抛出异常
        if (goods == null || goods.getStockCount() <= 0) {
            throw new RuntimeException("已售罄!");
        }

        //如果有库存,则创建秒杀商品订单
        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setId(idWorker.nextId());
        seckillOrder.setSeckillId(id);
        seckillOrder.setMoney(goods.getCostPrice());
        seckillOrder.setUserId(username);
        seckillOrder.setSellerId(goods.getSellerId());
        seckillOrder.setCreateTime(new Date());
        seckillOrder.setStatus("0");

        //将秒杀订单存到Redis中
        redisTemplate.boundHashOps("SeckillOrder").put(username, seckillOrder);
        //库存减少
        goods.setStockCount(goods.getStockCount() - 1);

        //判断当前商品是否还有库存
        if (goods.getStockCount()<=0){ //没有库存了

            //并且将商品数据同步到Mysql中
            seckillGoodsMapper.updateByPrimaryKeySelective(goods);

            //如果没有库存,则清空redis缓存中该商品
            redisTemplate.boundHashOps("SeckillGoods_"+time).delete(id);

            //如果有库存,,则将数据重置到Redis中
            redisTemplate.boundHashOps("SeckillGoods_"+time).put(id,goods);
        }
        return true;
    }
}
